package com.anteater.eattheant;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anteater.eattheant.util.Ranking;
import com.anteater.eattheant.util.Score;
import com.anteater.eattheant.util.SoundPlayer;

public class ScoreActivity extends Activity {

	private Ranking ranking;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		ranking = new Ranking(this);
		int level = getIntent().getExtras().getInt("level");
		short score = getIntent().getExtras().getShort("score");
		short min = getIntent().getExtras().getShort("min");

		setContentView(R.layout.score);
		TextView scoreText = (TextView) findViewById(R.id.textView1);
		scoreText.setTextColor(Color.BLACK);
		scoreText.setText(String.format(
				"Level %d: %d formiga(s) capturada(s)! São necessaria(s) %d formigas!",
				level, score, min));

		TextView message = (TextView) findViewById(R.id.textView2);
		message.setText("Tente novamente!");
		message.setTextColor(Color.BLACK);
		
		if (LevelActivity.NUMBER_LEVELS <= level) {
			message.setText("Parabéns! Você conseguiu alimentar completamente o Bandeira!");
			SoundPlayer.getInstance().playSound(this, R.raw.arroto_super);
		} else {
			if (ranking.getMaxLevel() >= level || score >= min) {
				if (ranking.getMaxLevel() >= level) {
					message.setText("O próximo nível já foi liberado.");
					SoundPlayer.getInstance().playSound(this, R.raw.arroto_pequeno);
				} else {
					if (LevelActivity.NUMBER_LEVELS >= level+1) {
						message.setText("O próximo nível foi liberado. Boa sorte!");
						SoundPlayer.getInstance().playSound(this, R.raw.arroto_medio);
					}
				}
				ranking.addScore(new Score(level, score));
			}
		}
		updateButtons(level, score, min);
		
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}
	
	private void updateButtons(int level, short score, short min) {
		final int actualLevel = level-1;
		final int nextLevel = level;
		
		ranking = new Ranking(this);
        Integer levelNum = ranking.getMaxLevel();
		
		Button playAgain = (Button) findViewById(R.id.button1);
		playAgain.setTextColor(Color.GREEN);
		playAgain.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), PlayActivity.class);
                intent.putExtra("level", actualLevel);
                startActivity(intent);			
                finish();
			}
		});
		
		Button next = (Button) findViewById(R.id.button2);
		next.setTextColor(Color.GREEN);
		next.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(), PlayActivity.class);
                intent.putExtra("level", nextLevel);
                startActivity(intent);	
                finish();
			}
		});
		if (levelNum < nextLevel && score < min) {
			next.setClickable(false);
			next.setEnabled(false);
			next.setTextColor(Color.RED);
		}
		
		if (LevelActivity.NUMBER_LEVELS <= actualLevel+1) {
			RelativeLayout layout = (RelativeLayout) findViewById(R.id.scorelayout);
			layout.removeView(next);
		}
	}
}
